package com.bjpowernode;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.Map;
import java.util.concurrent.Executor;

@SpringBootApplication
@EnableAsync // 开启异步支持
public class DlykServerApplication implements CommandLineRunner {

    public static void main(String[] args) {
//        SpringApplication.run(DlykServerApplication.class, args);
        ConfigurableApplicationContext applicationContext = SpringApplication.run(DlykServerApplication.class, args);
        Map<String, Executor> beansOfType = applicationContext.getBeansOfType(Executor.class);
        beansOfType.forEach((k , v) -> {
            System.out.println("beanName : " + k + ", bean object : " + v);
        });
    }

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 在main方法执行快结束的时候调用 , 进行一些初始化操作
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ObjectMapper mapper = new ObjectMapper();
        // 设置可见性
        mapper.setVisibility(PropertyAccessor.ALL , JsonAutoDetect.Visibility.ANY);
        // 激活类型
        mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING);
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(mapper, Object.class);
        redisTemplate.setValueSerializer(serializer);
    }
}
